#include "reflectorLight.h"

reflectorLight::reflectorLight()
{

}
reflectorLight::~reflectorLight()
{

}
void reflectorLight::getLightFromXML(std::ifstream &file)
{
	char line[MAX_LENGTH];
	std::string temp;
	std::string nazwaWezla;

	unsigned int i(0);

	while (nazwaWezla!="/light")
	{
		file.getline(line, MAX_LENGTH);
		temp.clear();
		temp.append(line);
		nazwaWezla.clear();

		i=0;
		while(temp[i] == 9 || temp[i] == 32)
			++i;
		if(temp[i] == '<')
		{
			std::string nazwaWezlaKoniec;
			std::string zawartoscWezla;
			++i;
			while(temp[i] != '>')
			{
				nazwaWezla.push_back(temp[i]);
				++i;
			}
			if(nazwaWezla == "ambient")
			{
				color tempCol;
				tempCol.getColorFromXML(file);
				this->ambient = tempCol;
				continue;
			}
			else if(nazwaWezla == "multiplyier")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/multiplyier")
					this->multiplyier = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "decayInverseConstParam")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/decayInverseConstParam")
					this->decayInverseConstParam = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "decayInverseLinParam")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/decayInverseLinParam")
					this->decayInverseLinParam = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "decayInverseSqrParam")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/decayInverseSqrParam")
					this->decayInverseSqrParam = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "ambienCoef")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/ambienCoef")
					this->ambienCoef = atof(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "position")
			{
				position.getPointFromXML(file);
				continue;
			}
			else if(nazwaWezla == "directrion")
			{
				directrion.getVector3FromXML(file);
				continue;
			}
			else if(nazwaWezla == "isAmbient")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/isAmbient")
					if(zawartoscWezla == "true")
						this->isAmbient = true;
					else
						this->isAmbient = false;
				continue;
			}
			else if(nazwaWezla == "castShadow")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/castShadow")
					if(zawartoscWezla == "true")
						this->castShadow = true;
					else
						this->castShadow = false;
				continue;
			}
			else if(nazwaWezla == "decay")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/decay")
					if(zawartoscWezla == "true")
						this->decay = true;
					else
						this->decay = false;
				continue;
			}
		}
	}
}
void reflectorLight::setOrjentation(point poz, vector3 dir)
{
	position = poz;
	directrion = dir;
	directrion.normalize();
}
void reflectorLight::setPosition(point poz)
{
	position = poz;
}
void reflectorLight::setDirection(vector3 dir)
{
	directrion = dir;
	directrion.normalize();
}
point reflectorLight::getPosition()
{
	return position;
}
vector3 reflectorLight::getDirection(point directionTo)
{
	vector3 temp(position, directionTo);
	temp.normalize();
	return temp;
}
photon reflectorLight::emitPhoton()
{
	return photon();
}